package com.hmyd.hmyd_android.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.Investment;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapCommonUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

public class InvestmentCurrentListAdapter extends BaseAdapter {
	private Context mContext;
	private List<Investment> cPlist;
	private LayoutInflater mInflater;
	
	private int max = 100;
	private int current = 0;
	
	BitmapUtils bitmapUtils;
	BitmapDisplayConfig displayConfig;

	public InvestmentCurrentListAdapter(Context context, List<Investment> plist) {
		super();

		bitmapUtils = new BitmapUtils(context);
		displayConfig = new BitmapDisplayConfig();
		displayConfig.setShowOriginal(false);
		displayConfig.setBitmapMaxSize(BitmapCommonUtils.getScreenSize(context));

		this.mContext = context;
		this.cPlist = plist;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return cPlist != null ? cPlist.size() : 0;
	}

	@Override
	public Investment getItem(int position) {
		return cPlist != null ? cPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (cPlist == null || getItem(position) == null) {
			return convertView;
		}

		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_investment_personal_notesl, null);
			holder = new ViewHolder();
			//holder.type = (TextView) convertView.findViewById(R.id.type);
			//holder.title = (TextView) convertView.findViewById(R.id.title);
			//holder.type_tag = (ImageView) convertView.findViewById(R.id.tag_image);
			holder.interest_rate = (TextView) convertView.findViewById(R.id.interest_rate);
			holder.interest_rate_tag = (TextView) convertView.findViewById(R.id.interest_rate_tag);
			holder.interest_rate_unit = (TextView) convertView
					.findViewById(R.id.interest_rate_unit);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.time_unit = (TextView) convertView.findViewById(R.id.time_unit);
			holder.investment_amount = (TextView) convertView.findViewById(R.id.investment_amount);
			holder.investment_amount_unit = (TextView) convertView.findViewById(R.id.investment_amount_unit);
			holder.timeProgressBar = (ProgressBar) convertView.findViewById(R.id.timeProgressBar);
			holder.itemTagIconTop = (ImageView) convertView.findViewById(R.id.itemTagIconTop);
			holder.investInfo = (TextView) convertView.findViewById(R.id.investInfo);
			holder.smartWay = (TextView) convertView.findViewById(R.id.smartWay);
			holder.repayMethod = (TextView) convertView.findViewById(R.id.repayMethod);
			holder.date = (TextView) convertView.findViewById(R.id.date);
			holder.qian = (TextView) convertView.findViewById(R.id.qian);
			holder.typeFrame = (TextView) convertView.findViewById(R.id.typeFrame);
			holder.date_unit = (TextView) convertView.findViewById(R.id.date_unit);
			//holder.interest_accrual = (TextView) convertView.findViewById(R.id.interest_accrual);
			
			// holder.total = (TextView) convertView.findViewById(R.id.total);
			// holder.total_unit = (TextView)
			// convertView.findViewById(R.id.total_unit);

			holder.tags_label = (TextView) convertView.findViewById(R.id.tags_label);

			holder.type_title = (RelativeLayout) convertView.findViewById(R.id.type_title);
			holder.normalLayout = (LinearLayout) convertView
					.findViewById(R.id.view_investment_normal);
			holder.phbLayout = (RelativeLayout) convertView
					.findViewById(R.id.view_investment_grpj_top);
			holder.phbLeftLayout = (RelativeLayout) convertView.findViewById(R.id.grpj_left);
			// 票惠宝T+1
			holder.grpjLayout = (RelativeLayout) convertView
					.findViewById(R.id.view_investment_grpj);
			holder.grpjRate = (TextView) convertView.findViewById(R.id.grpj_rate);
			holder.grpj_image_tag = (FrameLayout) convertView.findViewById(R.id.grpj_image_tag);

			holder.money = (TextView) convertView.findViewById(R.id.money);
			holder.sui = (TextView) convertView.findViewById(R.id.sui);
			holder.day = (TextView) convertView.findViewById(R.id.day);
			holder.grpj_button = (ImageView) convertView.findViewById(R.id.grpj_button);
			holder.hot = (TextView) convertView.findViewById(R.id.grpj_hot);
			holder.per = (TextView) convertView.findViewById(R.id.per);
			holder.grpj_jiaxi = (TextView) convertView.findViewById(R.id.interest_rate_tag);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Investment investment = getItem(position);
		if (investment != null) {
			if (investment.type.equals("grpj") && investment.period == 1) {// 票惠宝T+1
				
				holder.grpjLayout.setVisibility(View.GONE);
				holder.normalLayout.setVisibility(View.VISIBLE);

				holder.hot.setBackgroundResource(R.drawable.chan_img_hotorange);
				holder.grpj_image_tag.setBackgroundResource(R.drawable.chan_img_piaoorange);
				holder.grpj_button.setBackgroundResource(R.drawable.chan_img_orange);

				if (investment.rate.contains("%") && investment.rate.contains("+")) {//加息标
					holder.grpjRate.setText(investment.rate.split("%")[0]);
					holder.grpj_jiaxi.setText(investment.rate.split("%")[1] + "%");
					holder.grpj_jiaxi.setVisibility(View.VISIBLE);
				} else if (investment.rate.contains("+")) {
					holder.grpjRate.setText(investment.rate.split("[+]")[0]);
					holder.grpj_jiaxi.setText("+" + investment.rate.split("[+]")[1] + "%");
					holder.grpj_jiaxi.setVisibility(View.VISIBLE);
				} else {
					holder.grpj_jiaxi.setVisibility(View.GONE);
					holder.grpjRate.setText(investment.rate);
				}

				setText(investment, holder, 1);
			} else if (investment.type.equals("grpj") && investment.period == 30) {// 票惠宝T+30
				
				holder.grpjLayout.setVisibility(View.GONE);
				holder.normalLayout.setVisibility(View.VISIBLE);
				holder.grpj_jiaxi.setTextColor(mContext.getResources().getColor(R.color.grpj_rate));
				if (investment.rate.contains("%") && investment.rate.contains("+")) {//加息标
					holder.grpjRate.setText(investment.rate.split("%")[0]);
					holder.grpj_jiaxi.setText(investment.rate.split("%")[1] + "%");
					holder.grpj_jiaxi.setVisibility(View.VISIBLE);
				} else if (investment.rate.contains("+")) {
					holder.grpjRate.setText(investment.rate.split("[+]")[0]);
					holder.grpj_jiaxi.setText("+" + investment.rate.split("[+]")[1] + "%");
					holder.grpj_jiaxi.setVisibility(View.VISIBLE);
				} else {
					holder.grpj_jiaxi.setVisibility(View.GONE);
					holder.grpjRate.setText(investment.rate);
				}

				holder.phbLayout.setBackgroundColor(mContext.getResources().getColor(
						R.color.grpj_bg));
				holder.phbLeftLayout.setBackgroundColor(mContext.getResources().getColor(
						R.color.grpj_left));
				//holder.grpjRate.setTextColor(mContext.getResources().getColor(R.color.grpj_rate));
				holder.grpj_button.setBackgroundResource(R.drawable.chan_img_blue);
				holder.hot.setBackgroundResource(R.drawable.chan_img_hotblue);
				holder.grpj_image_tag.setBackgroundResource(R.drawable.chan_img_piaoblue);
				holder.per.setTextColor(mContext.getResources().getColor(R.color.grpj_rate));
				setText(investment, holder,30);
			} else {
				holder.grpjLayout.setVisibility(View.VISIBLE);
				holder.normalLayout.setVisibility(View.GONE);
				holder.grpj_image_tag.setVisibility(View.VISIBLE);
				holder.grpj_button.setVisibility(View.VISIBLE);
			}

			if (investment.rate.contains("%") && investment.rate.contains("+")) {
				holder.interest_rate.setText(investment.rate.split("%")[0]);
				holder.interest_rate_tag.setText(investment.rate.split("%")[1] + "%");
				holder.interest_rate_tag.setVisibility(View.VISIBLE);
			} else if (investment.rate.contains("+")) {
				holder.interest_rate.setText(investment.rate.split("[+]")[0]);
				holder.interest_rate_tag.setText("+" + investment.rate.split("[+]")[1] + "%");
				holder.interest_rate_tag.setVisibility(View.VISIBLE);
			} else {
				holder.interest_rate_tag.setVisibility(View.GONE);
				holder.interest_rate.setText(investment.rate);
			}

			holder.time.setText(investment.period + "");
			holder.date.setText(investment.period + "");
			holder.typeFrame.setText(investment.period + "");
			holder.time_unit.setText(Utils.periodConversion(investment.periodUnit));
			holder.money.setText(investment.investInfo);
			holder.smartWay.setText(investment.smartWay);
			holder.sui.setText(investment.smartWay);
			holder.day.setText(investment.repayMethod);
			holder.repayMethod.setText(investment.repayMethod);
			
			holder.investment_amount.setText(investment.minInvestAmount + "");
			holder.qian.setText(investment.minInvestAmount + "" );
			holder.date_unit.setText(Utils.periodConversion(investment.periodUnit));
			//holder.interest_accrual.setText(investment.interestMode);

			double purchasedAmount = Double.valueOf(investment.purchasedAmount);
			double totalAmount = Double.valueOf(investment.totalAmount);
			
			holder.timeProgressBar.setVisibility(View.VISIBLE);
			holder.timeProgressBar.setEnabled(false);
			int i = Integer.parseInt(investment.progress);
			holder.timeProgressBar.setProgress(i);
			//holder.timeProgressBar.setBackgroundColor(mContext.getResources().getColor(R.color.white));

			holder.tags_label.setText(investment.progress + "%");

			holder.tags_label.setBackgroundDrawable(null);

			holder.interest_rate
					.setTextColor(mContext.getResources().getColor(R.color.text_orange));
			holder.interest_rate_tag.setTextColor(mContext.getResources().getColor(
					R.color.text_orange));
			//holder.time.setTextColor(mContext.getResources().getColor(R.color.text_orange));
			// holder.total.setTextColor(mContext.getResources().getColor(R.color.text_orange));

			if (!TextUtils.isEmpty(investment.itemTagIcon)) {
				Bitmap bmp = bitmapUtils.getBitmapFromMemCache(investment.itemTagIcon,
						displayConfig);

			}

			if ("full".equals(investment.itemStatus) || "repay".equals(investment.itemStatus)
					|| "success".equals(investment.itemStatus)) {
				holder.interest_rate.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				holder.interest_rate_tag.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				holder.interest_rate_unit.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));
				//holder.time.setTextColor(mContext.getResources().getColor(
				//		R.color.home_tab_text_normal));
				holder.time_unit.setTextColor(mContext.getResources().getColor(
						R.color.home_tab_text_normal));

//				holder.type.setTextColor(mContext.getResources().getColor(
//						R.color.home_tab_text_normal));
//				holder.type.setBackgroundDrawable(mContext.getResources().getDrawable(
//						R.drawable.shape_investment_type_s));
				
				holder.tags_label.setText("");
				holder.tags_label.setBackgroundDrawable(mContext.getResources().getDrawable(
						R.drawable.tou_lei_guang));
			} else if ("reserve".equals(investment.itemStatus)) {

				// float size =
				// TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11,
				// mContext.getResources().getDisplayMetrics());
				holder.tags_label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
				holder.tags_label.setText("敬请\n期待");
			} else if ("order".equals(investment.itemStatus)) {

				// float size =
				// TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11,
				// mContext.getResources().getDisplayMetrics());
				holder.tags_label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
				holder.tags_label.setText("立即\n预约");
			}
		}

		return convertView;
	}
	
	Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			current = current + 1;
			handler.postDelayed(runnable, 100);
		}
	};

	/**
	 * 设置字体大小和颜色
	 * 
	 * @param investment
	 * @param holder
	 */
	private void setText(Investment investment, ViewHolder holder, int type) {

		if (type == 1) {
			/*
			 * if (Utils.getSDKVersion() > 11) {
			 * Utils.changeSizeAndColor(investment.investInfo, holder.money, 26,
			 * 36,mContext.getResources().getColor(R.color.text_orange));
			 * Utils.changeSizeAndColor(investment.smartWay, holder.sui, 26,
			 * 36,mContext.getResources().getColor(R.color.text_orange));
			 * Utils.changeSizeAndColor(investment.repayMethod, holder.day, 26,
			 * 36,mContext.getResources().getColor(R.color.text_orange)); } else
			 * { Utils.changeSizeAndColor(investment.investInfo, holder.money,
			 * 16, 26,mContext.getResources().getColor(R.color.text_orange));
			 * Utils.changeSizeAndColor(investment.smartWay, holder.sui, 16,
			 * 26,mContext.getResources().getColor(R.color.text_orange));
			 * Utils.changeSizeAndColor(investment.repayMethod, holder.day, 16,
			 * 26,mContext.getResources().getColor(R.color.text_orange)); }
			 */

			Utils.changeTextSize(investment.investInfo, holder.money, mContext.getResources()
					.getColor(R.color.text_orange));
			Utils.changeTextSize(investment.smartWay, holder.sui,
					mContext.getResources().getColor(R.color.text_orange));
			Utils.changeTextSize(investment.repayMethod, holder.day, mContext.getResources()
					.getColor(R.color.text_orange));
		} else if (type == 30) {
			/*
			 * if (Utils.getSDKVersion() > 11) {
			 * Utils.changeSizeAndColor(investment.investInfo, holder.money, 26,
			 * 36,mContext.getResources().getColor(R.color.grpj_rate));
			 * Utils.changeSizeAndColor(investment.smartWay, holder.sui, 26,
			 * 36,mContext.getResources().getColor(R.color.grpj_rate));
			 * Utils.changeSizeAndColor(investment.repayMethod, holder.day, 26,
			 * 36,mContext.getResources().getColor(R.color.grpj_rate)); } else {
			 * Utils.changeSizeAndColor(investment.investInfo, holder.money, 16,
			 * 26,mContext.getResources().getColor(R.color.grpj_rate));
			 * Utils.changeSizeAndColor(investment.smartWay, holder.sui, 16,
			 * 26,mContext.getResources().getColor(R.color.grpj_rate));
			 * Utils.changeSizeAndColor(investment.repayMethod, holder.day, 16,
			 * 26,mContext.getResources().getColor(R.color.grpj_rate)); }
			 */

			Utils.changeTextSize(investment.investInfo, holder.money, mContext.getResources()
					.getColor(R.color.grpj_rate));
			Utils.changeTextSize(investment.smartWay, holder.sui,
					mContext.getResources().getColor(R.color.grpj_rate));
			Utils.changeTextSize(investment.repayMethod, holder.day, mContext.getResources()
					.getColor(R.color.grpj_rate));
		}
	

	}

	class ViewHolder {
		//public TextView title;
		public TextView type;
		//public ImageView type_tag;
		public TextView interest_rate;
		public TextView interest_rate_tag;
		public TextView interest_rate_unit;
		public TextView time;
		public TextView time_unit;
		public TextView investment_amount;
		public TextView investment_amount_unit;
		public TextView date;
		public TextView qian;
		public TextView typeFrame;
		public TextView date_unit;
		//public TextView interest_accrual;
		//public TextView tags_labels;
		public ProgressBar timeProgressBar;
		public TextView tv_progress;
		public ImageView itemTagIconTop;
		public TextView investInfo;
		public TextView smartWay;
		public TextView repayMethod;
		
		
		// public TextView total;
		// public TextView total_unit;

		// public Button tags_layout;
		public TextView per;// 百分号
		public TextView tags_label;

		public LinearLayout normalLayout;
		public RelativeLayout phbLayout;// 颜色区分t+1,t+30
		public RelativeLayout phbLeftLayout;// 颜色区分t+1,t+30
		// 票惠宝
		public RelativeLayout grpjLayout;
		public RelativeLayout type_title;
		public TextView grpjRate;
		public TextView money;
		public TextView sui;
		public TextView day;
		public TextView hot;// 热门
		public FrameLayout grpj_image_tag;// 图标区分t+1,t+30
		public ImageView grpj_button;// 图标区分t+1,t+30
		public TextView grpj_jiaxi;

	}
}
